package cs275.ants.graph;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import junit.framework.TestCase;
import cs275.ants.graph.MazeCell.Wall;
import cs275.ants.graph.base.Graph;
import cs275.ants.graph.base.GraphEdge;
import cs275.ants.graph.base.GraphVertex;


public class MazeTest extends TestCase {

	public void testMaze2()  { // test with mock objects
		MazeCell[][] cells = new MazeCell[5][5]; 
		for (int row=0; row<5; row++) { 
			for (int col=0; col<5; col++) {
//				MazeCell newCell = new MazeCell(5,5);
				MazeCell newCell = new MazeCell();
				cells[row][col] = newCell;			
				newCell.row = row;
				newCell.column = col;
				if (col != 0) { 
					newCell.knockDownWall(cells[row][col-1]);
				}
			}
		}
		cells[0][4].knockDownWall(cells[1][4]);	
		cells[2][4].knockDownWall(cells[3][4]);
	
		cells[1][0].knockDownWall(cells[2][0]);
		cells[3][0].knockDownWall(cells[4][0]);		
		
	 	Maze maze = new Maze(5, 5, cells);
	 	
		Graph mazeGraph = maze.getGraph(); 
		Set<GraphVertex> allVertices2 = mazeGraph.getAllVertices();
		Iterator<GraphVertex> iter2 = allVertices2.iterator();

		// first edge 
		GraphVertex currentVertex = iter2.next(); 
		HashSet<GraphEdge> edges = mazeGraph.getEdgesByVertex(currentVertex);
		assertEquals(edges.size(), 1);
		
		while (iter2.hasNext()) { 
			currentVertex = iter2.next(); 
			edges = mazeGraph.getEdgesByVertex(currentVertex);
			if (iter2.hasNext()) { 
				assertEquals(edges.size(), 2);
			} else {
				assertEquals(edges.size(), 1);
			}
		}
	}
		
	
	public void testMaze() { 
		//Resources resources = new Resources();
		MazeGenerator mazeGenerator = new MazeGenerator();
		Maze maze = mazeGenerator.generate(3, 5);
		
		int numNoEdgeVertex = 0;
		int numIntactCell = 0;

		for (int row=0; row < maze.getHeight(); row++) {
			for (int col=0; col < maze.getWidth(); col++) {
				if (maze.getCell(row, col).hasAllWalls()) {
					numIntactCell++;
				}
			}
		}
		
		Graph mazeGraph = maze.getGraph(); 
		Set<GraphVertex> allVertices2 = mazeGraph.getAllVertices();
		Iterator<GraphVertex> iter2 = allVertices2.iterator();
		while (iter2.hasNext()) { 
			GraphVertex currentVertex = iter2.next(); 
			HashSet<GraphEdge> edges = mazeGraph.getEdgesByVertex(currentVertex);
			//assertFalse(edges.isEmpty());			
			if (edges.isEmpty()) {
				numNoEdgeVertex++;
			} 
		}		
		
		assertEquals(numNoEdgeVertex, 0);
		assertEquals(numIntactCell, 0);
	} 

	public void testGetNeighbors() {
		// Need to add this method
	}
	
	public void testGetGraph() {
		MazeCell cellNW = new MazeCell(0, 0, Wall.LEFT, Wall.UP, Wall.RIGHT, Wall.NONE);
		MazeCell cellN = new MazeCell(0, 1, Wall.LEFT, Wall.UP, Wall.RIGHT, Wall.NONE);
		MazeCell cellNE = new MazeCell(0, 2, Wall.LEFT, Wall.UP, Wall.RIGHT, Wall.NONE);
		
		MazeCell cellW = new MazeCell(1, 0, Wall.LEFT, Wall.NONE, Wall.NONE, Wall.NONE);
		MazeCell cellC = new MazeCell(1, 1, Wall.NONE, Wall.NONE, Wall.NONE, Wall.NONE);
		MazeCell cellE = new MazeCell(1, 2, Wall.NONE, Wall.NONE, Wall.RIGHT, Wall.NONE);
		
		MazeCell cellSW = new MazeCell(2, 0, Wall.LEFT, Wall.NONE, Wall.RIGHT, Wall.DOWN);
		MazeCell cellS = new MazeCell(2, 1, Wall.LEFT, Wall.NONE, Wall.RIGHT, Wall.DOWN);
		MazeCell cellSE = new MazeCell(2, 2, Wall.LEFT, Wall.NONE, Wall.RIGHT, Wall.DOWN);

		MazeCell cells[][] = new MazeCell[3][3];
		cells[0][0] = cellNW; 
		cells[0][1] = cellN;
		cells[0][2] = cellNE;
		cells[1][0] = cellW; 
		cells[1][1] = cellC;
		cells[1][2] = cellE;
		cells[2][0] = cellSW; 
		cells[2][1] = cellS; 
		cells[2][2] = cellSE; 
				
		Maze maze = new Maze(3, 3, cells);
		Graph graph = maze.getGraph();
		
		GraphVertex v1 = graph.getVertex(0, 0); 
		Set<GraphVertex> set1 = graph.getNeighborsByVertex(v1);
		Iterator<GraphVertex> iter = set1.iterator(); 
		while (iter.hasNext()) { 
			GraphVertex current = iter.next(); 
			assertEquals(current.getRow(), 1); 
			assertEquals(current.getCol(), 0);
		}

		GraphVertex vc = graph.getVertex(1,1); 
		assertEquals(vc.getRow(), cellC.row);
		assertEquals(vc.getCol(), cellC.column);
				
		Set<GraphVertex> setc = graph.getNeighborsByVertex(vc);
		Iterator<GraphVertex> iterc = setc.iterator(); 
		while (iterc.hasNext()) { 
			GraphVertex current = iterc.next();
			System.out.println(current.getRow() + " " + current.getCol());
//			assertEquals(current.getRow(), 1); 
//			assertEquals(current.getCol(), 0);
		}
		
		assertSame(graph, maze.getGraph());
	}
}
